<?php
namespace app\service;

use think\facade\Cache;

class EthnicityService
{
    protected $topviewApiService;

    public function __construct(TopviewApiService $topviewApiService)
    {
        $this->topviewApiService = $topviewApiService;
    }

    /**
     * 获取人种列表
     * @param string $scene 使用场景
     * @param int $pageNo 页码
     * @param int $pageSize 每页条数
     * @return array
     * @throws \Exception
     */
    public function getEthnicityList(string $scene, int $pageNo = 1, int $pageSize = 10): array
    {
        return $this->topviewApiService->getEthnicityList($scene, $pageNo, $pageSize);
    }

    /**
     * 验证scene参数
     * @param string $scene
     * @return bool
     */
    public function validateScene(string $scene): bool
    {
        return in_array($scene, ['video_avatar', 'product_avatar']);
    }

    /**
     * 获取支持的场景列表
     * @return array
     */
    public function getSupportedScenes(): array
    {
        return [
            'video_avatar' => '视频数字人',
            'product_avatar' => '商品数字人'
        ];
    }

    /**
     * 根据人种ID获取人种信息（从缓存的列表中查找）
     * @param string $ethnicityId
     * @param string $scene
     * @return array|null
     */
    public function getEthnicityById(string $ethnicityId, string $scene): ?array
    {
        try {
            $ethnicityList = $this->getEthnicityList($scene);
            
            foreach ($ethnicityList as $ethnicity) {
                if ($ethnicity['ethnicityId'] === $ethnicityId) {
                    return $ethnicity;
                }
            }
            
            return null;
        } catch (\Exception $e) {
            return null;
        }
    }
} 